package com.example.springbootrabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * tang
 * delay延迟队列
 * 调用不成功
 * 有另一个办法使用rabbitmq_delayed_message_exchange插件，云笔记里有例子，有空调试起来
 */

@Service
public class RabbitMqDelayInitService {


    //延迟队列
    @Bean
    public Queue delayQueue() {
        //return new Queue(Constant.DELAY_QUEUE, true);

        Map<String, Object> params = new HashMap<>();
        params.put("x-message-ttl", 5 * 1000);//设置 Queue 延迟时间
        return new Queue(Constant.DELAY_QUEUE, true, false, false, params);
    }

    //延迟队列交换器, x-delayed-type 和 x-delayed-message 固定
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(Constant.DELAY_EXCHANGG, "x-delayed-message", true, false, args);
    }

    //延迟队列绑定自定义交换器
    //tang 这个地方初始化失败导致整个应用无法启动完成
    /*
    @Bean
    public Binding delayBinding(Queue delayQueue, CustomExchange delayExchange) {
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(Constant.DELAY_QUEUE).noargs();
    }*/




}
